# 多进程版本,运算释放内存
import datetime, time
from concurrent.futures import (
    ProcessPoolExecutor,
    as_completed,
    ThreadPoolExecutor,
    wait,
    Future,
)

# from threading import Lock as Lock_t
from multiprocessing import Lock as Lock_p
import numpy, pysnooper

# 设置进程锁
plock = Lock_p()

# 设置目标数值
R1: float = 17150314

# 设置搜索范围,价格
A_range = numpy.arange(0.001, 3.50, 0.001)  # armCpu价格
B_range = numpy.arange(0, 0.001, 0.001)  # x86Cpu价格
C_range = numpy.arange(0.001, 4.00, 0.001)  # ARM 内存价格
D_range = numpy.arange(0, 0.001, 0.001)  # X86 内存价格
E_range = numpy.arange(0.001, 0.09, 0.001)  # 存储价格

# 设置硬件数量
A: int = 16040  # armCpu
B: int = 0  # x86Cpu
C: float = 60589.33  # ARM 内存
D: int = 0  # X86 内存
E: float = 3564769.28  # 存储


def r1_r2_subtraction(r2: float) -> float:
    global R1
    return R1 - r2


def calculate_r2(a: float, b: float, c: float, d: float, e: float) -> dict:
    global A, B, C, D, E
    res: float = (A * a + B * b + C * c + D * d + E * e) * 365
    dictA: dict = {
        "AP": a,
        "BP": b,
        "CP": c,
        "DP": d,
        "EP": e,
        "res": res,  # 本次计算的总值
        "abs": float(abs(r1_r2_subtraction(res))),  # R1-R2后的绝对值
    }
    return dictA


def checker(big_dict: dict, _dict):
    """检查者,对比哪个字典abs值更小并保存"""
    if big_dict["abs"] == 0:
        return _dict
    elif big_dict["abs"] > _dict["abs"]:
        return _dict
    else:
        return big_dict


def comput_for(a: float) -> dict:
    """循环计算主要函数1"""
    dict3 = {"abs": 0}
    for b in B_range:
        for c in C_range:
            for d in D_range:
                for e in E_range:
                    _a = calculate_r2(a, b, c, d, e)
                    if dict3["abs"] == 0:
                        dict3 = _a
                    elif dict3["abs"] > _a["abs"]:
                        dict3 = _a

    return dict3


# @pysnooper.snoop()
def main() -> None:
    dict2 = {"abs": 0}
    with ProcessPoolExecutor(max_workers=7) as pool:
        ptask_list = [pool.submit(comput_for, i) for i in A_range]
        # wait(ptask_list, return_when="ALL_COMPLETED")
        for _future in as_completed(ptask_list):
            _d = _future.result()
            plock.acquire()
            if dict2["abs"] == 0:
                dict2 = _d
                print(dict2)
            elif dict2["abs"] > _d["abs"]:
                dict2 = _d
                print(dict2)            
            plock.release()

    print("如果程序一开始就看到这段文字说明主线程没有阻塞，程序错误了")
    print("最终结算结果>>>",dict2)


if __name__ == "__main__":
    print("开始运行")
    timeStart = time.time()
    main()
    timeEnd = time.time()
    print("结束运行,总运行秒数为", timeEnd - timeStart)
